/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.template.helper;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.FreemarkerHelper;
import com.apache.info.util.FileOperate;
import com.apache.tools.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成页面实现类
 *
 * @author dyhou 创建时间：2016-9-30
 */
public class PageTempManager extends SupperTemplateManager {

    public PageTempManager(PubSortInfoManager sortInfoManager, PubMetadataManager metadataManager) {
        this.init(sortInfoManager, metadataManager);
    }

    public boolean createPage(Map<String, Object> param) {
        boolean mark = false;
        List<PubSortInfo> list = (List<PubSortInfo>) param.get("sortList");
        String prefix = String.valueOf(param.get("projectName")) + "/pages/";
        String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix;
        //		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.iussoft.utr中的utr
        String sysName = String.valueOf(param.get("sysCode"));
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            ParamsVo vo = new ParamsVo();
            PubMetadata metadata = new PubMetadata();
            FileOperate.getInstance().newCreateFolder(xmlPath);
            FileOperate.getInstance().newCreateFolder(xmlPath + "jsp/");
            metadata.setSortId("param_manager");
            vo.setObj(metadata);
            List pms = (List) metadataManager.getList(vo);
            InfoReleaseManager irM = (InfoReleaseManager) SpringContextLoader
                    .getBean("infoReleaseManager");
            for (PubSortInfo map : list) {
                String reqObjName = map.getObjName();
                data.put("objName", reqObjName);
                data.put("sysName", sysName);
                data.put("sort", map);
                metadata.setSortId(String.valueOf(map.getSortId()));
                vo.setObj(metadata);
                List mts = (List) metadataManager.getList(vo);
                data.put("metadatas", mts);
                data.put("projectName", param.get("projectName"));
                for (int i = 0; i < mts.size(); i++) {
                    PubMetadata mtp = (PubMetadata) mts.get(i);
                    if ("1".equals(String.valueOf(mtp.getDataRestrict()))) {
                        data.put("primaryKeyId", mtp.getDataAttr());
                        data.put("dataPrimaryKey", mtp.getDataAttr());
                    }
                }
                //				String result = FreemarkerHelper.instance().getTemplateStr("views/objName-edit-html.inc", data);
                //				result = result.replace("###", "$");
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-edit.html", result);
                //				result = FreemarkerHelper.instance().getTemplateStr("views/objName-list-html-inc", data);
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-list.html", result);
                //				result = FreemarkerHelper.instance().getTemplateStr("views/objName-view-html.inc", data);
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-view.html", result);
                String result = FreemarkerHelper.instance()
                        .getTemplateStr("views/objName-list.inc", data);
                result = result.replace("###", "$");
                FileOperate.getInstance().newCreateFile(xmlPath + reqObjName + "-list.jsp", result);
                String modelTypes[] = { "i_" + reqObjName, "u_" + reqObjName, "s_" + reqObjName };
                String FormNames[] = { "addForm", "editForm", "searchForm" };
                ParamsVo pvo = new ParamsVo();

                for (int i = 0; i < 3; i++) {
                    Map<String, String> pmMap = new HashMap<String, String>();
                    pmMap.put("infoId", Validator.generate());
                    pmMap.put("modelTypes", modelTypes[i]);
                    pmMap.put("formName", FormNames[i]);
                    pmMap.put("delStatus", "F");
                    pmMap.put("createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                    pmMap.put("pageName", reqObjName + "-list");
                    pmMap.put("tableName", "trp_param_manager");
                    pvo.setParams("metadatas", pms);
                    pvo.setParams("params", pmMap);
                    irM.saveInfo(pvo);
                }

            }

            mark = true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成页面失败", e);
        }
        return mark;
    }
}
